const app = getApp();
let timer = null;

Page({

        data: {
            swiperList: [{
                type: 'image',
                url: "https://gd-hbimg.huaban.com/55067d1b053ca36cf1f9acf5fa0622a88146d5ae3c7b2-ksTmGA_fw1200webp",
                // target:'1',
                target: 'http://baidu.com'
            }, {
                type: 'image',
                url: "https://gd-hbimg.huaban.com/102d7b669d4ee47a9e6fc605da61a9155039b4c939d56-oZJZyD_fw1200webp",
                target: "2",
            }, {

                type: 'image',
                url: "https://gd-hbimg.huaban.com/4beadaed948e49647f69b96a9730bbf9a561766d449ca-Unypjw_fw1200webp",
                target: "3",
            }, ],
            autoPlay: true,
            direction: 'left',
            interval: 3000,
            currData: {},
            navHeight: app.globalData.navHeight,
            navTop: app.globalData.navTop,
            navObj: app.globalData.navObj,
            navObjWid: app.globalData.navObjWi,
        },

        itemClickfour: function () {
            wx.navigateTo({
                url: '../route4/route4',
            })
        },

        itemClickfive: function () {
            wx.navigateTo({
                url: '../route5/route5',
            })
        },

        itemClicksix: function () {
            wx.navigateTo({
                url: '../route6/route6',
            })
        },

        itemClicksiven: function () {
            wx.navigateTo({
                url: '../route7/route7',
            })
        },

        itemClickeight: function () {
            wx.navigateTo({
                url: '../route8/route8',
            })
        },

        itemClicknine: function () {
            wx.navigateTo({
                url: '../route9/route9',
            })
        },
        swiperClick: function (e) {
            const index = e.currentTarget.dataset.index;
            if (index === 0) {
                wx.navigateTo({
                    url: '../route1/route1', // 替换为你要跳转的新页面路径
                });
            }
            if (index === 1) { // 注意这里数组下标从0开始，所以第二张图片的索引是1
                wx.navigateTo({
                    url: '../route2/route2', // 替换为你要跳转的新页面路径
                });
            }
            if (index === 2) {
                wx.navigateTo({
                    url: '../route3/route3', // 替换为你要跳转的新页面路径
                });
            }
        },
        onLoad: function (options) {
            this.tauchSwiper('swiperList');
        },


        onShow: function () {

        },
        // 初始化tauchSwiper
        tauchSwiper(name) {
            let list = this.data[name];
            for (let i = 0; i < list.length; i++) {
                // Math.abs(x) 函数返回指定数字 “x“ 的绝对值
                list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
                list[i].mLeft = i - parseInt(list.length / 2)
            }
            this.setData({
                swiperList: list
            })
        },

        // tauchSwiper触摸开始
        tauchStart(e) {
            this.setData({
                tauchStart: e.touches[0].pageX
            })
        },
        // tauchSwiper计算方向
        tauchMove(e) {
            this.setData({
                direction: e.touches[0].pageX - this.data.tauchStart > 0 ? 'right' : 'left'
            })
        },
        // tauchSwiper计算滚动
        tauchEnd(e) {
            let direction = this.data.direction;
            let list = this.data.swiperList;
            if (direction == 'right') {
                let mLeft = list[0].mLeft;
                let zIndex = list[0].zIndex;
                for (let i = 1; i < list.length; i++) {
                    list[i - 1].mLeft = list[i].mLeft
                    list[i - 1].zIndex = list[i].zIndex
                }
                list[list.length - 1].mLeft = mLeft;
                list[list.length - 1].zIndex = zIndex;
                this.setData({
                    swiperList: list
                })
            } else {
                let mLeft = list[list.length - 1].mLeft;
                let zIndex = list[list.length - 1].zIndex;
                for (let i = list.length - 1; i > 0; i--) {
                    list[i].mLeft = list[i - 1].mLeft
                    list[i].zIndex = list[i - 1].zIndex
                }
                list[0].mLeft = mLeft;
                list[0].zIndex = zIndex;
                this.setData({
                    swiperList: list
                })
            }
        }
    },


)